import {observable, action} from "mobx";
import {ListAdvanced} from "weaver-mobile-page";

export default class MyAssetPageStore {
    @observable showCheck = false;
    @observable selectedTabIndex = 0;
    @observable isRight = false;
    @observable hrmid = "";

    @observable assetListMine = new ListAdvanced.Store({route: "/myassetsingle", id: "0"});
    @observable assetListAll = new ListAdvanced.Store({route: "/myassetsingle", id: "1"});
    @observable conditionFetchOptions = {
        url: "/api/cpt/moblie/capitalportal/getCptListCondition",
        method: "GET"
    };
    @observable dataFetchOptions = {
        url: "/api/cpt/moblie/capitalportal/getCapitalList",
        method: "GET"
    };
    @observable dataFetchOptionsMine = {
        url: "/api/cpt/moblie/capitalportal/getCapitalList",
        method: "GET",
        params: {type: "mycpt"}
    };

    @action
    onTabChange = (index) => {
        this.selectedTabIndex = index;
        this.init(this.hrmid)
    };


    @action
    init = (hrmid) => {
        this.hrmid = hrmid;
        if (this.selectedTabIndex === 0) {
            this.assetListMine.clear();
            this.getAssetList(this.assetListMine, {type: "mycpt", hrmid: this.hrmid});
        }else{
            this.assetListAll.clear();
            this.getAssetList(this.assetListAll);
        }
    };

    @action
    getAssetList = (list, params = {}) => {
        list.getData({url: "/api/cpt/moblie/capitalportal/getCapitalList", method: "GET", params: params})
        list.searchAdvancedStore.getData(this.conditionFetchOptions).then((form) => {
            if (list.searchParams.name) {
                form.updateField("name", list.searchParams.name);
            }
        });
    };

    @action
    onSearch = () => {
        let list, params = {};
        if (this.selectedTabIndex === 1) {
            list = this.assetListAll;
        } else {
            list = this.assetListMine;
            params = {type: "mycpt", hrmid: this.hrmid};
        }
        const {searchAdvancedStore, listViewStore, searchParams} = list;
        // 搜索框等搜索数据
        Object.assign(params, searchParams);
        // 获取高级搜索参数
        if (searchAdvancedStore) {
            const {formParams} = searchAdvancedStore.form;
            Object.assign(params, formParams);
        }
        // 清除列表数据
        listViewStore.clearData();
        // 带参数请求列表数据
        this.getAssetList(list, params);
    };
}
